﻿#include "Settings.h"
#include "MirSounds/SoundManager.h"
#include "../Shared/Language.h"

using namespace Client::MirSounds;

namespace Client
{

int Settings::ScreenWidth = 1024;
int Settings::ScreenHeight = 768;
InIReader *Settings::Reader = new InIReader(R"(.\Mir2Config.ini)");
bool Settings::_useTestConfig = false;

    bool Settings::getUseTestConfig()
    {
        return _useTestConfig;
    }

    void Settings::setUseTestConfig(bool value)
    {
        if (value == true)
        {
            Reader = new InIReader(R"(.\Mir2Test.ini)");
        }
        _useTestConfig = value;
    }

const std::string DataPath, MapPath, SoundPath, ExtraDataPath, ShadersPath, MonsterPath, GatePath, FlagPath, NPCPath, CArmourPath, CWeaponPath, CWeaponEffectPath, CHairPath, AArmourPath, AWeaponPath, AHairPath, ARArmourPath, ARWeaponPath, ARHairPath, CHumEffectPath, AHumEffectPath, ARHumEffectPath, MountPath, FishingPath, PetsPath, TransformPath, TransformMountsPath, TransformEffectPath, TransformWeaponEffectPath, Settings::MouseCursorPath = R"(.\Data\Cursors\)";
bool Settings::LogErrors = true;
bool Settings::LogChat = true;
int Settings::RemainingErrorLogs = 100;
bool Settings::FullScreen = true;
bool Settings::Borderless = true;
bool Settings::TopMost = true;
std::string Settings::FontName = "Tahoma";
float Settings::FontSize = 8.0F;
bool Settings::UseMouseCursors = true;
bool Settings::FPSCap = true;
int Settings::MaxFPS = 100;
int Settings::Resolution = 1024;
bool Settings::DebugMode = false;
bool Settings::UseConfig = false;
std::string Settings::IPAddress = "127.0.0.1";
int Settings::Port = 7000;
int Settings::SoundOverLap = 3;
unsigned char Settings::_volume = 100;

    unsigned char Settings::getVolume()
    {
        return _volume;
    }

    void Settings::setVolume(unsigned char value)
    {
        if (_volume == value)
        {
            return;
        }

        _volume = static_cast<unsigned char>(value > 100 ? 100 : value);

        if (_volume == 0)
        {
            SoundManager::setVol(-10000);
        }
        else
        {
            SoundManager::setVol(static_cast<int>(-3000 + (3000 * (_volume / 100))));
        }
    }

unsigned char Settings::_musicVolume = 100;

    unsigned char Settings::getMusicVolume()
    {
        return _musicVolume;
    }

    void Settings::setMusicVolume(unsigned char value)
    {
        if (_musicVolume == value)
        {
            return;
        }

        _musicVolume = static_cast<unsigned char>(value > 100 ? 100 : value);

        if (_musicVolume == 0)
        {
            SoundManager::setMusicVol(-10000);
        }
        else
        {
            SoundManager::setMusicVol(static_cast<int>(-3000 + (3000 * (_musicVolume / 100))));
        }
    }

std::string Settings::AccountID = "";
std::string Settings::Password = "";
bool Settings::SkillMode = false;
bool Settings::SkillBar = true;
bool Settings::Effect = true;
bool Settings::LevelEffect = true;
bool Settings::DropView = true;
bool Settings::NameView = true;
bool Settings::HPView = true;
bool Settings::TransparentChat = false;
bool Settings::ModeView = false;
bool Settings::DuraView = false;
bool Settings::DisplayDamage = true;
bool Settings::TargetDead = false;
bool Settings::HighlightTarget = true;
bool Settings::ExpandedBuffWindow = true;
std::vector<std::vector<int>> Settings::SkillbarLocation =
{
    {0, 0},
    {216, 0}
};
std::vector<int> Settings::TrackedQuests(5);
bool Settings::ShowNormalChat = true;
bool Settings::ShowYellChat = true;
bool Settings::ShowWhisperChat = true;
bool Settings::ShowLoverChat = true;
bool Settings::ShowMentorChat = true;
bool Settings::ShowGroupChat = true;
bool Settings::ShowGuildChat = true;
bool Settings::FilterNormalChat = false;
bool Settings::FilterWhisperChat = false;
bool Settings::FilterShoutChat = false;
bool Settings::FilterSystemChat = false;
bool Settings::FilterLoverChat = false;
bool Settings::FilterMentorChat = false;
bool Settings::FilterGroupChat = false;
bool Settings::FilterGuildChat = false;
bool Settings::P_Patcher = true;
std::string Settings::P_Host = R"(http://mirfiles.com/mir2/cmir/patch/)";
std::string Settings::P_PatchFileName = R"(PList.gz)";
bool Settings::P_NeedLogin = false;
std::string Settings::P_Login = "";
std::string Settings::P_Password = "";
std::string Settings::P_ServerName = "";
std::string Settings::P_BrowserAddress = "https://launcher.mironline.co.uk/web/";
std::string Settings::P_Client = Application::StartupPath + "\\";
bool Settings::P_AutoStart = false;

    void Settings::Load()
    {
        GameLanguage::LoadClientLanguage(R"(.\Language.ini)");

        if (!FileSystem::directoryExists(DataPath))
        {
            FileSystem::createDirectory(DataPath);
        }
        if (!FileSystem::directoryExists(MapPath))
        {
            FileSystem::createDirectory(MapPath);
        }
        if (!FileSystem::directoryExists(SoundPath))
        {
            FileSystem::createDirectory(SoundPath);
        }

        //Graphics
        FullScreen = Reader->ReadBoolean("Graphics", "FullScreen", FullScreen);
        Borderless = Reader->ReadBoolean("Graphics", "Borderless", Borderless);
        TopMost = Reader->ReadBoolean("Graphics", "AlwaysOnTop", TopMost);
        FPSCap = Reader->ReadBoolean("Graphics", "FPSCap", FPSCap);
        Resolution = Reader->ReadInt32("Graphics", "Resolution", Resolution);
        DebugMode = Reader->ReadBoolean("Graphics", "DebugMode", DebugMode);
        UseMouseCursors = Reader->ReadBoolean("Graphics", "UseMouseCursors", UseMouseCursors);

        //Network
        UseConfig = Reader->ReadBoolean("Network", "UseConfig", UseConfig);
        if (UseConfig)
        {
            IPAddress = Reader->ReadString("Network", "IPAddress", IPAddress);
            Port = Reader->ReadInt32("Network", "Port", Port);
        }

        //Logs
        LogErrors = Reader->ReadBoolean("Logs", "LogErrors", LogErrors);
        LogChat = Reader->ReadBoolean("Logs", "LogChat", LogChat);

        //Sound
        setVolume(Reader->ReadByte("Sound", "Volume", getVolume()));
        SoundOverLap = Reader->ReadInt32("Sound", "SoundOverLap", SoundOverLap);
        setMusicVolume(Reader->ReadByte("Sound", "Music", getMusicVolume()));

        //Game
        AccountID = Reader->ReadString("Game", "AccountID", AccountID);
        Password = Reader->ReadString("Game", "Password", Password);

        SkillMode = Reader->ReadBoolean("Game", "SkillMode", SkillMode);
        SkillBar = Reader->ReadBoolean("Game", "SkillBar", SkillBar);
        //SkillSet = Reader.ReadBoolean("Game", "SkillSet", SkillSet);
        Effect = Reader->ReadBoolean("Game", "Effect", Effect);
        LevelEffect = Reader->ReadBoolean("Game", "LevelEffect", Effect);
        DropView = Reader->ReadBoolean("Game", "DropView", DropView);
        NameView = Reader->ReadBoolean("Game", "NameView", NameView);
        HPView = Reader->ReadBoolean("Game", "HPMPView", HPView);
        ModeView = Reader->ReadBoolean("Game", "ModeView", ModeView);
        FontName = Reader->ReadString("Game", "FontName", FontName);
        TransparentChat = Reader->ReadBoolean("Game", "TransparentChat", TransparentChat);
        DisplayDamage = Reader->ReadBoolean("Game", "DisplayDamage", DisplayDamage);
        TargetDead = Reader->ReadBoolean("Game", "TargetDead", TargetDead);
        HighlightTarget = Reader->ReadBoolean("Game", "HighlightTarget", HighlightTarget);
        ExpandedBuffWindow = Reader->ReadBoolean("Game", "ExpandedBuffWindow", ExpandedBuffWindow);
        DuraView = Reader->ReadBoolean("Game", "DuraWindow", DuraView);

        for (int i = 0; i < SkillbarLocation.size() / 2; i++)
        {
            SkillbarLocation[i][0] = Reader->ReadInt32("Game", "Skillbar" + std::to_string(i) + "X", SkillbarLocation[i][0]);
            SkillbarLocation[i][1] = Reader->ReadInt32("Game", "Skillbar" + std::to_string(i) + "Y", SkillbarLocation[i][1]);
        }

        //Chat
        ShowNormalChat = Reader->ReadBoolean("Chat", "ShowNormalChat", ShowNormalChat);
        ShowYellChat = Reader->ReadBoolean("Chat", "ShowYellChat", ShowYellChat);
        ShowWhisperChat = Reader->ReadBoolean("Chat", "ShowWhisperChat", ShowWhisperChat);
        ShowLoverChat = Reader->ReadBoolean("Chat", "ShowLoverChat", ShowLoverChat);
        ShowMentorChat = Reader->ReadBoolean("Chat", "ShowMentorChat", ShowMentorChat);
        ShowGroupChat = Reader->ReadBoolean("Chat", "ShowGroupChat", ShowGroupChat);
        ShowGuildChat = Reader->ReadBoolean("Chat", "ShowGuildChat", ShowGuildChat);

        //Filters
        FilterNormalChat = Reader->ReadBoolean("Filter", "FilterNormalChat", FilterNormalChat);
        FilterWhisperChat = Reader->ReadBoolean("Filter", "FilterWhisperChat", FilterWhisperChat);
        FilterShoutChat = Reader->ReadBoolean("Filter", "FilterShoutChat", FilterShoutChat);
        FilterSystemChat = Reader->ReadBoolean("Filter", "FilterSystemChat", FilterSystemChat);
        FilterLoverChat = Reader->ReadBoolean("Filter", "FilterLoverChat", FilterLoverChat);
        FilterMentorChat = Reader->ReadBoolean("Filter", "FilterMentorChat", FilterMentorChat);
        FilterGroupChat = Reader->ReadBoolean("Filter", "FilterGroupChat", FilterGroupChat);
        FilterGuildChat = Reader->ReadBoolean("Filter", "FilterGuildChat", FilterGuildChat);

        //AutoPatcher
        P_Patcher = Reader->ReadBoolean("Launcher", "Enabled", P_Patcher);
        P_Host = Reader->ReadString("Launcher", "Host", P_Host);
        P_PatchFileName = Reader->ReadString("Launcher", "PatchFile", P_PatchFileName);
        P_NeedLogin = Reader->ReadBoolean("Launcher", "NeedLogin", P_NeedLogin);
        P_Login = Reader->ReadString("Launcher", "Login", P_Login);
        P_Password = Reader->ReadString("Launcher", "Password", P_Password);
        P_AutoStart = Reader->ReadBoolean("Launcher", "AutoStart", P_AutoStart);
        P_ServerName = Reader->ReadString("Launcher", "ServerName", P_ServerName);
        P_BrowserAddress = Reader->ReadString("Launcher", "Browser", P_BrowserAddress);

        if (!StringHelper::endsWith(P_Host, "/"))
        {
            P_Host += "/";
        }
        if (P_Host.StartsWith("www.", StringComparison::OrdinalIgnoreCase))
        {
            P_Host.insert(0, "http://");
        }
        if (P_BrowserAddress.StartsWith("www.", StringComparison::OrdinalIgnoreCase))
        {
            P_BrowserAddress.insert(0, "http://");
        }

        //Temp check to update everyones address
        if (StringHelper::toLower(P_Host) == "http://mirfiles.co.uk/mir2/cmir/patch/")
        {
            P_Host = "http://mirfiles.com/mir2/cmir/patch/";
        }
    }

    void Settings::Save()
    {
        //Graphics
        Reader->Write("Graphics", "FullScreen", FullScreen);
        Reader->Write("Graphics", "Borderless", Borderless);
        Reader->Write("Graphics", "AlwaysOnTop", TopMost);
        Reader->Write("Graphics", "FPSCap", FPSCap);
        Reader->Write("Graphics", "Resolution", Resolution);
        Reader->Write("Graphics", "DebugMode", DebugMode);
        Reader->Write("Graphics", "UseMouseCursors", UseMouseCursors);

        //Sound
        Reader->Write("Sound", "Volume", getVolume());
        Reader->Write("Sound", "Music", getMusicVolume());

        //Game
        Reader->Write("Game", "AccountID", AccountID);
        Reader->Write("Game", "Password", Password);
        Reader->Write("Game", "SkillMode", SkillMode);
        Reader->Write("Game", "SkillBar", SkillBar);
        //Reader.Write("Game", "SkillSet", SkillSet);
        Reader->Write("Game", "Effect", Effect);
        Reader->Write("Game", "LevelEffect", LevelEffect);
        Reader->Write("Game", "DropView", DropView);
        Reader->Write("Game", "NameView", NameView);
        Reader->Write("Game", "HPMPView", HPView);
        Reader->Write("Game", "ModeView", ModeView);
        Reader->Write("Game", "FontName", FontName);
        Reader->Write("Game", "TransparentChat", TransparentChat);
        Reader->Write("Game", "DisplayDamage", DisplayDamage);
        Reader->Write("Game", "TargetDead", TargetDead);
        Reader->Write("Game", "HighlightTarget", HighlightTarget);
        Reader->Write("Game", "ExpandedBuffWindow", ExpandedBuffWindow);
        Reader->Write("Game", "DuraWindow", DuraView);

        for (int i = 0; i < SkillbarLocation.size() / 2; i++)
        {

            Reader->Write("Game", "Skillbar" + std::to_string(i) + "X", SkillbarLocation[i][0]);
            Reader->Write("Game", "Skillbar" + std::to_string(i) + "Y", SkillbarLocation[i][1]);
        }

        //Chat
        Reader->Write("Chat", "ShowNormalChat", ShowNormalChat);
        Reader->Write("Chat", "ShowYellChat", ShowYellChat);
        Reader->Write("Chat", "ShowWhisperChat", ShowWhisperChat);
        Reader->Write("Chat", "ShowLoverChat", ShowLoverChat);
        Reader->Write("Chat", "ShowMentorChat", ShowMentorChat);
        Reader->Write("Chat", "ShowGroupChat", ShowGroupChat);
        Reader->Write("Chat", "ShowGuildChat", ShowGuildChat);

        //Filters
        Reader->Write("Filter", "FilterNormalChat", FilterNormalChat);
        Reader->Write("Filter", "FilterWhisperChat", FilterWhisperChat);
        Reader->Write("Filter", "FilterShoutChat", FilterShoutChat);
        Reader->Write("Filter", "FilterSystemChat", FilterSystemChat);
        Reader->Write("Filter", "FilterLoverChat", FilterLoverChat);
        Reader->Write("Filter", "FilterMentorChat", FilterMentorChat);
        Reader->Write("Filter", "FilterGroupChat", FilterGroupChat);
        Reader->Write("Filter", "FilterGuildChat", FilterGuildChat);

        //AutoPatcher
        Reader->Write("Launcher", "Enabled", P_Patcher);
        Reader->Write("Launcher", "Host", P_Host);
        Reader->Write("Launcher", "PatchFile", P_PatchFileName);
        Reader->Write("Launcher", "NeedLogin", P_NeedLogin);
        Reader->Write("Launcher", "Login", P_Login);
        Reader->Write("Launcher", "Password", P_Password);
        Reader->Write("Launcher", "ServerName", P_ServerName);
        Reader->Write("Launcher", "Browser", P_BrowserAddress);
        Reader->Write("Launcher", "AutoStart", P_AutoStart);
    }

    void Settings::LoadTrackedQuests(const std::string &Charname)
    {
        //Quests
        for (int i = 0; i < TrackedQuests.size(); i++)
        {
            TrackedQuests[i] = Reader->ReadInt32("Q-" + Charname, "Quest-" + std::to_string(i), -1);
        }
    }

    void Settings::SaveTrackedQuests(const std::string &Charname)
    {
        //Quests
        for (int i = 0; i < TrackedQuests.size(); i++)
        {
            Reader->Write("Q-" + Charname, "Quest-" + std::to_string(i), TrackedQuests[i]);
        }
    }
}
